import {receiveOrderById,cancelOrderById,getOrderById,getAllOrders,postOrder,updateOrderById,deleteOrderById} from "@/api/api";
import {getAllOrdersByUsername} from "../../../src/api/api";
let nowUsername= sessionStorage.getItem("nowUsername");
export default {
  name: "viewOrders",
  data(){
    return{

      //----测试用----start
      newOrder: {
        username: "j2ee",
        realname: "杨睿睿",
        orderDate: "1992-04-09 12:20:03",
        orderStatus: "NF",
        shipCountry: "中国",
        shipCity: "牡丹江市",
        address: "海外重庆市海淀区",
        courier: "申通",
        totalPrice: 138.03,
        payType: "支付宝",
        itemList: [
          {
            itemId: "EST-4",
            quantity: 41,
            price: 39,
            iName: "史能接自备表",
            iImage: "http://dummyimage.com/400x400",
            iDescn: "97"
          },
          {
            itemId: "EST-17",
            quantity: 44,
            price: 11,
            iName: "容花但去西日标",
            iImage: "http://dummyimage.com/400x400",
            iDescn: "76"
          }
        ]
      },
      updateOrder: {
        orderId: 1032,
        username: "j2ee",
        realname: "杨睿睿",
        orderDate: "2022-04-09 12:20:03",
        orderStatus: "NF",
        shipCountry: "中国",
        shipCity: "牡丹江市",
        address: "海外重庆市海淀区",
        courier: "申通",
        totalPrice: 138.03,
        payType: "支付宝",
        itemList: [
          {
            itemId: "EST-10",
            quantity: 41,
            price: 39,
            iName: "史能",
            iImage: "http://dummyimage.com/400x400",
            iDescn: "97"
          },
          {
            itemId: "EST-1",
            quantity: 4,
            price: 11,
            iName: "容花",
            iImage: "http://dummyimage.com/400x400",
            iDescn: "76"
          },
          {
            itemId: "EST-11",
            quantity: 50,
            price: 18,
            iName: "容花",
            iImage: "http://dummyimage.com/400x400",
            iDescn: "76"
          }
        ]
      },
      //----测试用----end
      message:"", //用来提示操作成功
      //暂定义
      username : "h",
      showOrderList:[

      ],
      allOrderList:[

      ],
      deliveredOrderList:[

      ],
      notShippedOrderList:[

      ],
      waitReviewOrderList:[

      ],
      rejectedOrderList:[

      ]
    }
  },
  mounted() { //mounted是vue中的一个钩子函数，一般在初始化页面完成后，再对dom节点进行相关操作。
    this.fetchData();
    this.getAllOrderList();
    // location.reload();
  },
  methods: {
    async fetchData() {
      console.log("---测试order部分---");

      // let orders = await getAllOrders();
      // console.log(orders);
      // let nowUsername = sessionStorage.getItem("nowUsername");
      // console.log("nowUsername是: " + nowUsername);
      //
      // let orders = await getAllOrders();
      // console.log(orders);

      // postOrder(this.newOrder).then((res)=>{
      //   console.log(res);
      // }).catch((res) => {
      //   alert("错误：" + res);
      // });

      // updateOrderById(1032, this.updateOrder).then((res)=>{
      //   console.log(res);
      // }).catch((res) => {
      //   alert("错误：" + res);
      // });

      // deleteOrderById(1032).then((res)=>{
      //   console.log(res);
      // }).catch((res) => {
      //   alert("错误：" + res);
      // });

      // cancelOrderById(1031);
      // receiveOrderById(1031);
      // getOrderById(1031).then((res)=>{
      //   console.log(res);
      // }).catch((res) => {
      //   alert("错误：" + res);
      // });

    },
    //得到所有订单
    getAllOrderList() {
      getAllOrdersByUsername(nowUsername).then((res) => {
        console.log(res);
        console.log(res.data);
        var deliveredNum = 0, nshipNum = 0, waitReNum = 0, rejectedNum = 0;
        for (var i = 0; i < res.data.data.length; i++) {
          this.allOrderList = res.data.data;
          if (this.allOrderList[i].orderStatus == "YF") {
            this.deliveredOrderList[deliveredNum] = this.allOrderList[i];
            deliveredNum++;
          } else if (this.allOrderList[i].orderStatus == "NF") {
            this.notShippedOrderList[nshipNum] = this.allOrderList[i];
            nshipNum++;
          } else if (this.allOrderList[i].orderStatus == "YS") {
            this.waitReviewOrderList[waitReNum] = this.allOrderList[i];
            waitReNum++;
          } else {
            this.rejectedOrderList[rejectedNum] = this.allOrderList[i];
            rejectedNum++;
          }
        }
      })
    },
    //取消订单
    CancelOrder(orderId) {
      cancelOrderById(orderId).then((res) => {
        this.getAllOrderList();
          alert("Cancel order successfully.");
        }
      ).catch((res) => {
        alert("错误：" + res);
      });
    },
    ConfirmReceipt(orderId) {
      receiveOrderById(orderId).then((res) => {
        this.getAllOrderList();
        alert("Confirm receipt successfully.");
      }).catch((res) => {
        alert("错误：" + res);
      });
    },
    //评价
    EvaluateOrder(orderId) {

      // receiveOrderById(orderId).then((res) => {
      //   // alert("Default evaluation success.");
      //   this.getAllOrderList();
      // }).catch((res) => {
      //   alert("错误：" + res);
      // });
    },
    showOrder(itemList,status) {
      this.showOrderList = itemList;
      for(var i = 0;i < this.showOrderList.length;i++){
        this.showOrderList[i]['orderStatus'] = status;
        console.log(status);
        console.log(this.showOrderList[i].orderStatus);
      }
    }
  }
}
